{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "kxu1Gfhx1pHg"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/jeffheaton/app_generative_ai/blob/main/assignments/assignment_yourname_class10.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "ZbNbAV281pHh"
      },
      "source": [
        "# T81-559: Applications of Generative AI\n",
        "* Instructor: [Jeff Heaton](https://sites.wustl.edu/jeffheaton/), McKelvey School of Engineering, [Washington University in St. Louis](https://engineering.wustl.edu/Programs/Pages/default.aspx)\n",
        "* For more information visit the [class website](https://sites.wustl.edu/jeffheaton/t81-558/).\n",
        "\n",
        "**Module 10 Assignment: StreamLit**\n",
        "\n",
        "**Student Name: Your Name**"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "XLFov09h18yC"
      },
      "source": [
        "# Google CoLab Instructions\n",
        "\n",
        "The following code ensures that Google CoLab is running and maps Google Drive if needed."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "AWGARRT92DrA"
      },
      "outputs": [],
      "source": [
        "import os\n",
        "\n",
        "try:\n",
        "    from google.colab import drive, userdata\n",
        "    drive.mount('/content/drive', force_remount=True)\n",
        "    COLAB = True\n",
        "    print(\"Note: using Google CoLab\")\n",
        "except:\n",
        "    print(\"Note: not using Google CoLab\")\n",
        "    COLAB = False\n",
        "\n",
        "# OpenAI Secrets\n",
        "if COLAB:\n",
        "    os.environ[\"OPENAI_API_KEY\"] = userdata.get('OPENAI_API_KEY')\n",
        "\n",
        "# Install needed libraries in CoLab\n",
        "if COLAB:\n",
        "    !pip install langchain langchain_openai openai streamlit"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "v2MPPX0c1pHi"
      },
      "source": [
        "# Assignment Submit Function\n",
        "\n",
        "You will submit the 10 programming assignments electronically.  The following submit function can be used to do this.  My server will perform a basic check of each assignment and let you know if it sees any basic problems.\n",
        "\n",
        "**It is unlikely that should need to modify this function.**"
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "import base64\n",
        "import os\n",
        "import numpy as np\n",
        "import pandas as pd\n",
        "import requests\n",
        "import PIL\n",
        "import PIL.Image\n",
        "import io\n",
        "\n",
        "# This function submits an assignment.  You can submit an assignment as much as you like, only the final\n",
        "# submission counts.  The paramaters are as follows:\n",
        "# data - List of pandas dataframes or images.\n",
        "# key - Your student key that was emailed to you.\n",
        "# course - The course that you are in, currently t81-558 or t81-559.\n",
        "# no - The assignment class number, should be 1 through 1.\n",
        "# source_file - The full path to your Python or IPYNB file.  This must have \"_class1\" as part of its name.\n",
        "# .             The number must match your assignment number.  For example \"_class2\" for class assignment #2.\n",
        "def submit(data,key,course,no,source_file=None):\n",
        "    if source_file is None and '__file__' not in globals(): raise Exception('Must specify a filename when a Jupyter notebook.')\n",
        "    if source_file is None: source_file = __file__\n",
        "    suffix = '_class{}'.format(no)\n",
        "    if suffix not in source_file: raise Exception('{} must be part of the filename.'.format(suffix))\n",
        "    with open(source_file, \"rb\") as image_file:\n",
        "        encoded_python = base64.b64encode(image_file.read()).decode('ascii')\n",
        "    ext = os.path.splitext(source_file)[-1].lower()\n",
        "    if ext not in ['.ipynb','.py']: raise Exception(\"Source file is {} must be .py or .ipynb\".format(ext))\n",
        "    payload = []\n",
        "    for item in data:\n",
        "        if type(item) is PIL.Image.Image:\n",
        "            buffered = BytesIO()\n",
        "            item.save(buffered, format=\"PNG\")\n",
        "            payload.append({'PNG':base64.b64encode(buffered.getvalue()).decode('ascii')})\n",
        "        elif type(item) is pd.core.frame.DataFrame:\n",
        "            payload.append({'CSV':base64.b64encode(item.to_csv(index=False).encode('ascii')).decode(\"ascii\")})\n",
        "    r= requests.post(\"https://api.heatonresearch.com/wu/submit\",\n",
        "        headers={'x-api-key':key}, json={ 'payload': payload,'assignment': no, 'course':course, 'ext':ext, 'py':encoded_python})\n",
        "    if r.status_code==200:\n",
        "        print(\"Success: {}\".format(r.text))\n",
        "    else: print(\"Failure: {}\".format(r.text))"
      ],
      "metadata": {
        "id": "-SXa26WQ9d-L"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "# Assignment Instructions\n",
        "\n",
        "Write a StreamLit application that prompts the user to enter a comma-separated list of ICD-10 codes and displays a table with the corresponding SNOMED codes with the human-readable name of the code. Use gpt-4o-mini to perform this translation.\n",
        "\n",
        "SNOMED (Systematized Nomenclature of Medicine) and ICD-10 (International Classification of Diseases, 10th Revision) are comprehensive systems for coding medical conditions and diseases. ICD-10, developed by the World Health Organization (WHO), is primarily used for diagnostic purposes in clinical and billing contexts. In contrast, SNOMED, managed by SNOMED International, provides a more detailed and granular representation of clinical information. Most ICD-10 codes map to SNOMED codes, allowing for more standardized exchange of medical data across different healthcare systems. This mapping enables healthcare providers to document patient conditions precisely, enhancing data interoperability and patient care.\n",
        "\n",
        "Some examples of ICD-10 codes include, with the corresponding SNOWMED code are:\n",
        "* R10.10: Upper abdominal pain\n",
        "    * SNOMED: 21522001 - Upper abdominal pain\n",
        "* R10.30: Lower abdominal pain\n",
        "    * SNOMED: 5008001 - Lower abdominal pain\n",
        "* E11.9: Type 2 diabetes mellitus without complications\n",
        "    * SNOMED: 44054006 - Diabetes mellitus type 2\n",
        "* Z20.828: Contact with and (suspected) exposure to other viral communicable diseases\n",
        "    * SNOMED: 443684005 - Exposure to communicable disease\n",
        "* U07.1: COVID-19, virus identified\n",
        "    * SNOMED: 840539006 - COVID-19\n",
        "* J06.9: Acute upper respiratory infection, unspecified\n",
        "    * SNOMED: 54150009 - Acute upper respiratory infection\n",
        "* I10: Essential (primary) hypertension\n",
        "    * SNOMED: 38341003 - Essential hypertension\n",
        "* M54.5: Low back pain\n",
        "    * SNOMED: 279039007 - Low back pain\n",
        "* R51: Headache\n",
        "    * SNOMED: 25064002 - Headache\n",
        "\n",
        "You can use these to test your program.\n",
        "\n",
        "Your program should look something like this:\n",
        "\n",
        "![Assignment 10](https://data.heatonresearch.com/images/wustl/app_genai/assignments/t81-559-10.jpg)\n",
        "\n",
        "### Solution"
      ],
      "metadata": {
        "id": "CA4KR0C19lLj"
      }
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "_d1jBuL-wiyh"
      },
      "outputs": [],
      "source": [
        "%%writefile app.py\n",
        "import streamlit as st\n",
        "\n",
        "st.write(\"Hello World\")"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "OWnPCS8v_iYS"
      },
      "source": [
        "Next, we obtain the password for our StreamLit server we are about to launch."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "Uisp3omywt5V"
      },
      "outputs": [],
      "source": [
        "!curl https://loca.lt/mytunnelpassword"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "AZHCdk7j_mUQ"
      },
      "source": [
        "We launch the StreamLit server and obtain its URL. You will need the above password when you access the URL it gives you."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "fap8dxKTw1xF"
      },
      "outputs": [],
      "source": [
        "!streamlit run app.py server1 &>/content/logs.txt &\n",
        "!npx --yes localtunnel --port 8501"
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "Submit your assignment (make sure to stop your StreamLit server first)."
      ],
      "metadata": {
        "id": "d5x0zqM5_ZP_"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Add your solution here, put your results into submit_df\n",
        "\n",
        "# This is your student key that I emailed to you at the beginnning of the semester.\n",
        "key = \"5iuwhudihwiao6dsfw7dE2ml08iNfVOg6l0O3M06\"  # This is an example key and will not work.\n",
        "\n",
        "# You must also identify your source file.  (modify for your local setup)\n",
        "file='/content/drive/MyDrive/Colab Notebooks/assignment_solution_class10.ipynb'  # Google CoLab\n",
        "\n",
        "# Note: no dataframe to submit, just your notebook\n",
        "submit(source_file=file,data=[],key=key,course='t81-559',no=10)"
      ],
      "metadata": {
        "id": "spKT9M4K_VZ_"
      },
      "execution_count": null,
      "outputs": []
    }
  ],
  "metadata": {
    "anaconda-cloud": {},
    "colab": {
      "provenance": []
    },
    "kernelspec": {
      "display_name": "Python 3 (ipykernel)",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.12.4"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}